/*
 */

package com.jams.goodox.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.jams.goodox.R;
import com.jams.goodox.entity.ProductTypeInfo;
import com.jams.goodox.utils.AnimUtils;
import com.jams.goodox.utils.DensityUtil;
import com.jams.library.commonAdapter.ListCommonAdapter;
import com.jams.library.commonAdapter.ListCommonViewHolder;
import com.jams.library.widget.MyGridView;

import java.util.List;

/**
 * Created by jams on 2016/7/7.
 * ***
 * Tel: ***
 */

public class ShopTypePopupWindow {
    private static ShopTypePopupWindow instance = null;
    private int sHeight;
    private ListCommonAdapter adapter;
    private DataChangeListener menuDialogClickListener;
    private List<ProductTypeInfo> dataList;
    private boolean isEdit = false;
    private WindowManager.LayoutParams lp;
    private Window mWindow;
    private PopupWindow popupWindow;

    public static ShopTypePopupWindow getInstance() {
        if (instance == null) {
            instance = new ShopTypePopupWindow();
        }
        return instance;
    }

    int pos;

    public PopupWindow showShopTypePopupWindow(final View viewFather, final Context context, final List<ProductTypeInfo> dataList, final DataChangeListener menuDialogClickListener, final int selectPosition) {

        sHeight = DensityUtil.getDisplayHeight(context);
        this.dataList = dataList;
        pos = selectPosition;
        popupWindow = new PopupWindow();
        final View view = LayoutInflater.from(context).inflate(R.layout.product_type_dialog, null);
        MyGridView gvTypeList = (MyGridView) view.findViewById(R.id.gv_type_list);
        ImageView ivMengban = (ImageView) view.findViewById(R.id.iv_mengban);
        gvTypeList.setAdapter(new ListCommonAdapter<ProductTypeInfo>(context, dataList, R.layout.product_type_item) {
            @Override
            public void convert(ListCommonViewHolder helper, ProductTypeInfo item, final int position) {
                if (position == pos) {
                    helper.setBackground(R.id.tv_type_name, R.drawable.bottom_btn_2dp_main);
                } else {
                    helper.setBackground(R.id.tv_type_name, R.drawable.bottom_btn_2dp_shallow_gray);
                }
                helper.setText(R.id.tv_type_name, dataList.get(position).getCategoryName());
                helper.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int positions) {
                        pos = positions;
                        notifyDataSetChanged();
                        menuDialogClickListener.change(position);
                        dismissPop();
                    }
                });
            }
        });
        ivMengban.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissPop();
            }
        });
        int[] location = new int[2];
        viewFather.getLocationOnScreen(location);
        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, sHeight - location[1]- DensityUtil.dp2px(context,44));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00005600));    //要为popWindow设置一个背景才有效
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        menuDialogClickListener.dismiss();
                    }
                }, 100);

            }
        });
        //设置popupWindow显示的位置，参数依次是参照View，x轴的偏移量，y轴的偏移量
//        popupWindow.setAnimationStyle(R.style.Menu_PopuWindow_style);
        AnimUtils.startAlhpaAnim(ivMengban, 10);
        popupWindow.showAsDropDown(viewFather, -100, 0);
        return popupWindow;
    }

    public interface DataChangeListener {
        void change(int position);

        void dismiss();
    }

    public void dismissPop() {
        if (popupWindow != null) {
            popupWindow.dismiss();
        }
    }
}
